package com.test.base.netty.future_promise;

import io.netty.channel.EventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;

@Slf4j
public class UseFuture {
    /**
     * description future获取消息的方法是异步的
     * @author zken
     * @CreateDate 2024/11/20 16:25:49
     */
    public static void main(String[] args) {
        NioEventLoopGroup group = new NioEventLoopGroup();
        EventLoop eventLoop = group.next();

        Future<Integer> future = eventLoop.submit(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                log.debug("future中执行方法");
                Thread.sleep(2000);
                return 70;
            }
        });
        // 此行代码是获取不到结果的，会输出null值
        System.out.println(future.getNow());
        // 使用异步方法监听，（在异步的线程中-输出future的值）
        future.addListener(new GenericFutureListener<Future<? super Integer>>() {
            @Override
            public void operationComplete(Future<? super Integer> future) throws Exception {
                log.debug("接受结果为:{}", future.getNow());
            }
        });
    }
}
